import 'dart:convert';

import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttermarketingplus/constant/global_data.dart';
import 'package:fluttermarketingplus/generated/i18n.dart';
import 'package:fluttermarketingplus/utils/auto_layout.dart';
import 'package:fluttermarketingplus/utils/exit_app.dart';
import 'package:fluttermarketingplus/utils/permission_request.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'home_page/home_page.dart';
import 'my_page/my_page.dart';
import 'order_page.dart';

//底部菜单引导
class NavigatorIndex extends StatefulWidget {
  @override
  _NavigatorIndexState createState() => _NavigatorIndexState();
}

class _NavigatorIndexState extends State<NavigatorIndex> {
  final PageController _controller = PageController(initialPage: 0);
  int index = 0;
  SharedPreferences prefs;

  @override
  void initState() {
    initData();
    super.initState();
  }

  //初始化本地存储，如果有登陆页，防置到登陆页
  Future initData() async {
    prefs = await SharedPreferences.getInstance();
    GlobalData.prefs = prefs;
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return WillPopScope(
        child: Scaffold(
          body: PageView(
            physics: NeverScrollableScrollPhysics(),
            controller: _controller,
            children: <Widget>[HomePage(), OrderPage(), MyPage()],
          ),
          bottomNavigationBar: CupertinoTabBar(
            currentIndex: index,
            onTap: (i) {
              this.changeIndex(i);
            },
            items: [
              _bottomBarItem(S.of(context).home_navigator_home,
                  'navigator_home_select.png', 'navigator_home_unselect.png'),
              _bottomBarItem(
                  S.of(context).home_navigator_order,
                  'navigator_learning_select.png',
                  'navigator_learning_unselect.png'),
              _bottomBarItem(
                  '我的', 'navigator_me_select.png', 'navigator_me_unselect.png'),
            ],
          ),
        ),
        onWillPop: exitApp);
  }

  //底部单个BarItem
  BottomNavigationBarItem _bottomBarItem(
      String title, String selectImg, String unselectImg) {
    return BottomNavigationBarItem(
      activeIcon: Image.asset(
        'images/${selectImg}',
//        width: AutoLayout.instance.pxToDp(48),
        height: AutoLayout.instance.pxToDp(48),
      ),
      title: Text(
        title,
        style: TextStyle(fontSize: ScreenUtil().setSp(20)),
      ),
      icon: Image.asset(
        'images/${unselectImg}',
//        width: AutoLayout.instance.pxToDp(48),
        height: AutoLayout.instance.pxToDp(48),
      ),
    );
  }

  changeIndex(int _index) {
    _controller.jumpToPage(_index);
    this.setState(() {
      index = _index;
    });
  }
}
